import regeneratorRuntime from '../../utils/regeneratorRuntime.js'
import { data } from '../../utils/data.js'
import utils from '../../utils/util.js'
import Notify from '/vant-weapp/notify/notify'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    read_id: '',
    hash: '',
    menuList: data.meunList,
    swiperList: [],
    shopList: [],
    isShow: false,
    autoplay: false,
    loading: true,
    site: true,
    area_box: false,
    siteList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    this.setData({
      read_id: options.id ? options.id : '',
      hash: options.hash ? options.hash : ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow (t) {
    this.setData({ autoplay: true })
    app.index.getStart()
      .then(data=>{
        this._start(data)
        if (!data.multi_site) {
          this.getIndex()
        } else {
          wx.getLocation({
            type: 'gcj02',
            success:(res)=> {
              this.getIndex(res);
            },
            fail:(res)=> {
              app.toast('获取位置失败');
              this.setData({ loading: false });
            }
          })
        }
        if (t == 'Refresh') {
          setTimeout(() => {
            Notify({
              text: `最后一次刷新 ${utils.formatTime()}`,
              duration: 1000,
              selector: '#custom-selector',
              backgroundColor: '#fc643e'
            });
          }, 1000)
        }
      })
  },

  async getIndex(params={}) {
    const { read_id, hash} = this.data
    try {
      const data = await app.index.getHome(read_id, hash, params)
      this.setData({
        ...this._map(data),
        loading: false
      })
      wx.stopPullDownRefresh()
    } catch (err) {
      console.log(err)
      app.toast(err)
    }
  },

  _map(data) {
    return {
      shopList: [
        {
          title: '特惠活动',
          more_url: '/pages/shops/list/list',
          list: data.goods,
          type: 0,
        },{
          list: data.activity,
          type: 1,
        },{
          title: '积分商品',
          more_url: '/pages/shops/mall-list/index',
          list: data.mall,
          type: 0
        }
      ],
      site: data.site? true: false,
      siteList: data.site,
      curr_site: data.curr_site,
    }
  },

  _start(data) {
    this.setData({
      swiperList: data.banner.map(item => {
        return {
          id: item.id,
          img: item.icon,
          title: item.title,
          islink: item.islink,
          link: item.link,
          sort: item.sort
        }
      }),
      menuList: data.nav
    })
  },

  onSearch() {
    wx.navigateTo({
      url: `/pages/shops/search/index`,
    })
  },

  onSwiper({ detail: { link } }) {

    if (link) {
      if (link.substr(0, 8) == 'https://' || link.substr(0, 7) == 'http://') {
        wx.navigateTo({
          url: '/ju_sign/main/common/webview?link=' + utils.base64Encode(link)
        })
      } else {
        wx.navigateTo({
          url: link,
          fail:()=>{
            app.toast('跳转页面失败，请检查页面地址')
          }
        })
      }
    }
  },

  switchArea () {
    this.setData({
      area_box: true
    })
  },

  onAreaBoxClose () {
    this.setData({
      area_box: false
    })
  },

  doSwitch (e) {
    var id = e.currentTarget.dataset.id;
    app.index.getSwitch(id)
      .then(() => {
        this.setData({
          area_box: false
        });
        this.onShow();
      })
      .catch(err=>{
        this.setData({
          area_box: false
        });
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({ autoplay: false })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({ autoplay: false })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow('Refresh')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
